<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<title>appscript | 12. Command Examples</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css" media="all"><!--@import url(full.css);--></style>

</head>
<body>

<h1>12. Command Examples</h1>

<!-- top navigation -->
<div class="navbar">
	<a href="11_applicationcommands.html">Previous</a> | <a href="index.html">Up</a> | <a href="13_performanceissues.html">Next</a>
	
</div>

<!-- content -->
<div id="content">


<h2><code>get</code></h2>

<p>Get the name of every folder in the user's home folder:</p>

<pre><code>// tell application "Finder" to get name of every folder of home

[[[finder get: [[[FNApp home] folders] name]] send]</code></pre>

<p>Note that if the direct parameter is omitted from the argument list, the reference that the command is invoked on is used instead. For example, the above example would normally be written as:</p>

<pre><code>[[[[[finder home] folders] name] get] send]</code></pre>


<h2><code>set</code></h2>

<p>Set the content of a TextEdit document:</p>

<pre><code>// tell application "TextEdit" to set text of document 1 to "Hello World"

[[[[[[textedit documents] at: 1] text] set] to: @"Hello World"] send]</code></pre>


<h2><code>count</code></h2>

<p>Count the words in a TextEdit document:</p>

<pre><code>// tell application "TextEdit" to count words of document 1

[[[[[textedit documents] at: 1] words] count] send]</code></pre>

<p>Count the items in the current user's home folder:</p>

<pre><code>//tell application "Finder" to count items of home

[[[[finder home] count] each: [FNConstant item]] send]</code></pre>

<p>(Note that the <code>each</code> parameter is required in Finder's <code>count</code> command.)</p>


<h2><code>make</code></h2>

<p>Create a new TextEdit document:</p>

<pre><code>// tell application "TextEdit" to make new document &not;
//     with properties {text:"Hello World\n"}

[[[[textedit make]
             new_: [TEConstant document]]
   withProperties: [NSDictionary dictionaryWithObjectsAndKeys:
                    @"Hello World\n", [TEConstant text], nil]]
             send]</code></pre>

<p>Append text to a TextEdit document:</p>

<pre><code>// tell application "TextEdit" to make new paragraph &not;
//     at end of text of document 1 &not;
//     with properties {text:"Yesterday\nToday\nTomorrow\n"}

[[[[[textedit make]
              new_: [TEConstant paragraph]]
                at: [[[[TEApp documents] at: 1] text] end]]
          withData: @"Yesterday\nToday\nTomorrow\n"]
              send]</code></pre>


<h2><code>duplicate</code></h2>

<p>Duplicate a folder to a disk, replacing an existing item if one exists:</p>

<pre><code>// tell application "Finder" to &not;
//     duplicate folder "Projects" of home to disk "Work" with replacing

[[[[[finder home]
         folders] byName: @"Projects"] 
                            duplicate]
                                   to: [[FNApp disks] byName: @"Backup"]]
                            replacing: AEMTrue]
                                 send]</code></pre>


<h2><code>add</code></h2>

<p>Add every person with a known birthday to a group named "Birthdays":</p> 

<pre><code>// tell application "Address Book" to add &not;
//     every person whose birth date is not missing value &not;
//     to group "Birthdays"

[[[[[addressBook people]
                 byTest: [[ABIts birthDate] notEquals: [ABConstant missingValue]]]
                    add] to: [[ABApp groups] byName: @"Birthdays"]]
                   send]</code></pre>



<!-- TO DO: example with error handling -->

</div>

<!-- bottom navigation -->
<div class="navbar">
	<a href="11_applicationcommands.html">Previous</a> | <a href="index.html">Up</a> | <a href="13_performanceissues.html">Next</a>
	
</div>

<!--footer-->
<p class="footer">&copy; 2007 HAS</p>
</body>
</html>